package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.Employee;
import com.csi.domain.User;
import com.csi.pojo.PageBean;
import com.csi.pojo.Result;
import com.csi.service.EmployeeService;
import com.csi.service.UserService;
import com.csi.service.impl.EmployeeServiceImpl;
import com.csi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/user/findAllController")
public class FindAllController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        PrintWriter out = resp.getWriter();

        UserService userService = new UserServiceImpl();
        List<User> userList = null;

        try {
            userList = userService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            Result<Object> result = new Result<>();
            result.setCode(500);
            result.setMsg("服务器内部错误");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        Result<List<User>> result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setDatas(userList);

        out.print(JSON.toJSONString(result));

        out.flush();
        out.close();
    }
}

